﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web.UI.WebControls;
using System.IO;

using TLL.Log;
using TLL.UTL;

namespace TasksLog
{
    public partial class PaidTasksFrom : Form
    {
        public bool isSaved = false;
        public PaidTasksFrom()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            initializeTasks();
        }

        private void initializeTasks()
        {
            using (var repository = new LogRepository(Parametrs.LogURL, Parametrs.DebugURL, null))
            {
                var asd = new ListItem();
                var items = repository.Get().OrderBy(x => x.ID).Select(x => new ListItem
                {
                    Text = x.Text.Split(':')[0],
                    Value = x.ID.ToString(),
                    Enabled = x.Status == LogStatuses.Complite
                }).ToArray();
                foreach (var item in items)
                    CLBItems.Items.Add(item, item.Enabled);
            }
        }

        private void BtPaid_Click(object sender, EventArgs e)
        {
            this.SaveToNewLog();
            this.Close();
        }

        private void PaidTasksFrom_Load(object sender, EventArgs e)
        {
            
        }
        private bool SaveToNewLog()
        {
            var res = true;
            using (var repository = new LogRepository(Parametrs.LogURL, Parametrs.DebugURL, null))
            {
                var allItems = repository.Get();
                var items = new List<LogItem>();
                var selectItemsID = CLBItems.CheckedItems.Cast<ListItem>().Select(x => int.Parse(x.Value)).ToList();//Items.Cast<ListItem>().Where(x=>x.Selected).ToList();
                var select = CLBItems.Items.Cast<ListItem>().FirstOrDefault();
                foreach (var selectItemID in selectItemsID)
                    items.Add(allItems.FirstOrDefault(x=>x.ID == selectItemID));

                res = repository.CopyItemsToNewFile(Parametrs.LogsFolderURL + DateTime.Now.ToShortDateString() + ".data", items);
                res = repository.RemoveItems(items);
            }
            return res;
        }
    }
}
